package com.ceam.biz.activiti.controller;

import com.ceam.biz.activiti.consts.ActivitiConstant;
import com.ceam.biz.activiti.service.BizNodeService;
import com.ceam.biz.activiti.vo.ProcessNodeVo;
import com.ceam.core.base.api.R;
import org.activiti.bpmn.model.*;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author CeaM
 * @Description
 * @date 2020/9/28 15:26
 */
@RestController
@RequestMapping("/activiti/node")
public class BizNodeController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private BizNodeService bizNodeService;

    /**
     * 获取节点列表
     *
     * @param proDefId
     * @return
     */
    @GetMapping("list/{proDefId}")
    public R list(@PathVariable String proDefId)
    {
        List<ProcessNodeVo> list = new ArrayList<>();
        System.out.println("lalala Node List lalala");
        BpmnModel model = repositoryService.getBpmnModel(proDefId);
        if (model != null)
        {
            Collection<FlowElement> flowElements = model.getMainProcess().getFlowElements();
            for (FlowElement element : flowElements) {
                ProcessNodeVo node = new ProcessNodeVo();
                node.setNodeId(element.getId());
                node.setName(element.getName());
                if (element instanceof StartEvent)
                {
                    // 开始节点
                    node.setType(ActivitiConstant.NODE_TYPE_START);
                }
                else if (element instanceof UserTask)
                {
                    // 用户任务
                    node.setType(ActivitiConstant.NODE_TYPE_TASK);
                }
                else if (element instanceof EndEvent)
                {
                    // 结束
                    node.setType(ActivitiConstant.NODE_TYPE_END);
                }
                else
                {
                    // 排除其他连线或节点
                    continue;
                }
                list.add(node);
            }
        }
        Map<String, Object> map=new HashMap<>();
        map.put("rows", list);
        System.out.println("Node List====="+list);
        //R(code=0, msg=操作成功, total=0, data={rows=[ProcessNodeVo(nodeId=sid-7E6D6EB6-3EEE-4697-961C-F03B421B7CC2, name=null, type=0, roleIds=null, userIds=null, deptIds=null, deptHeader=false),
        //这里的数据结构比另一个项目多了“data”，故前端获取数据比另一个也要多出“data” -> res.data.rows.map
        System.out.println(R.ok(map));
        return R.ok(map);
    }

    /**
     * 获取节点属性
     *
     * @param nodeId
     * @return
     */
    @GetMapping("get/{nodeId}")
    public R get(@PathVariable String nodeId)
    {
        ProcessNodeVo node = new ProcessNodeVo();
        node.setNodeId(nodeId);
        // 设置关联角色，用户，负责人
        node = bizNodeService.setAuditors(node);
        return R.ok(node);
    }

    /**
     * 修改节点属性
     *
     * @param node
     * @return
     */
    @PostMapping("update")
    public R update(@RequestBody ProcessNodeVo node)
    {
        bizNodeService.updateBizNode(node);
        return R.ok();
    }
}
